<?php
require_once('diy.php');
require_once('util.php');
require_once('diy_image.php');
require_once('model.php');
require_once('anti_spam/spam_filter.php');

define('PHOTO_DIR', '../photos/');
if (isset($_POST['type'])) {
  call_user_func($_POST['type'], $_POST);  
} else if (isset($_GET['type'])) {
  call_user_func($_GET['type'], $_GET);
}

/* 
 comment update
 */
function register_email_alert() {
  return;
}

function cancel_email_notification() {
  return;
}

function write_report($data) {
  $fields = array('category',
  		  'subject',
  		  'details',
  		  'reporter',
  		  'email',
  		  'phone',
  		  'lat',
  		  'lng',
  		  'address1',
  		  'address2',
  		  'address3');

  $full_address = $data['address1'] . $data['address2'] . $data['address3'];
  $address_hash = md5($full_address);

  $input = array();

  foreach($fields as $field) {
    if (isset($data[$field])) {
      $input[$field] = trim($data[$field]);
    }
  }
  $input['address_hash'] = $address_hash;
  $input['created_time'] = get_current_time();
  $input['modified_time'] = get_current_time();

  $problems = new DiyModel("problems");

  $problems->save($input);


  $inserted_id = $problems->id;

  if ( isset($_FILES['photo']['tmp_name']) &&
       $_FILES['photo']['size'] != 0) {
    $diy_img = new DiyImage();
    $tmp_name = $_FILES['photo']['tmp_name'];
    $image_path = PHOTO_DIR . $inserted_id;

    if (!move_uploaded_file ( $tmp_name, $image_path . '_full')) 
      die("Upload failed");

    $diy_img->moveResizedImage($image_path . '_full', $image_path);
    if (!$diy_img->makeThumbnail($image_path, $image_path . '_thumb'))
      die("makeThumbnail Failed");
  }
  redirect("../view_report.php?id=" . $inserted_id);
}

function modify_report($data) {
  $problems = new DiyModel("problems");

  $input = array();
  $input['details'] = trim($data['details']);
  $input['subject'] = trim($data['subject']);
  $input['modified_time'] = get_current_time();

  $problems->updateAll($input, array('id' => $data['problem_id']));
  redirect("../view_report.php?id=" . $data['problem_id']);
}

function modify_update($data) {
  $updates = new DiyModel("updates");

  $input = array();
  $input['details'] = $data['details'];
  $updates->updateAll($input, array('id' => $data['id']));
  redirect("../view_report.php?id=" . $data['problem_id']);
}

function spam_check($keyword) {
  return (hangul_chk($keyword) &&
	  html_url_chk($keyword) &&
	  filter_chk($keyword));
}

function updates($data) {
  $input = array();
  
  $input['problem_id'] =  $data['problem_id'];
  $input['reporter'] =  trim($data['reporter']);
  $input['details'] =  trim($data['details']);
  $input['email'] =  trim($data['email']);
  $input['phone'] =  $data['phone'];
  $input['created_time'] = get_current_time();
  $input['modified_time'] = get_current_time();

  if (!spam_check($input['details']))
    redirect("../view_report.php?id=" . $data['problem_id']);

  if (trim($data['details']) == '' &&
      $_FILES['photo']['size'] == 0) {
    redirect("../view_report.php?id=" . $data['problem_id']);
  }

  $updates = new DiyModel("updates");
  $updates->save($input);
  $inserted_id = $updates->id;


  if ( isset($_FILES['photo']['tmp_name']) &&
       $_FILES['photo']['size'] != 0) {
    $diy_img = new DiyImage();

    $tmp_name = $_FILES['photo']['tmp_name'];
    $image_path = PHOTO_DIR . $input['problem_id'] . '_' . $inserted_id;


    if (!move_uploaded_file ( $tmp_name, $image_path . '_full')) 
      die("Upload failed");
    $diy_img->moveResizedImage($image_path . '_full', $image_path);
    if (!$diy_img->makeThumbnail($image_path, $image_path . '_thumb'))
      die("makeThumbnail Failed");
  }

  redirect("../view_report.php?id=" . $data['problem_id']);
}

function problem_state($data) {
  if (ADMIN) {
    $m = new DiyModel($data['model']);
    $m->id = $data['id'];
    $m->saveField('state', $data['state']);
    $m->saveField('modified_time', get_current_time());

    if (isset($_SERVER['HTTP_REFERER'])) 
      redirect($_SERVER['HTTP_REFERER']);
    else
      redirect("../admin");
  }
}

function delete($data) {
  if (ADMIN) {
    if ($data['model'] == 'updates') {
      $path = PHOTO_DIR . $data['problem_id'] . '_' . $data['id'];
      if ( file_exists($path . '_full') )
	unlink($path . '_full');
      if ( file_exists($path) )
	unlink($path);
      if ( file_exists($path . '_thumb') )
	unlink($path . '_thumb');
    } else if ($data['model'] == 'problems') {
      $path = PHOTO_DIR . $data['id'];
      if ( file_exists($path . '_full') )
	unlink($path . '_full');
      if ( file_exists($path) )
	unlink($path);
      if ( file_exists($path . '_thumb') )
	unlink($path . '_thumb');
    }

    $m = new DiyModel($data['model']);
    $m->remove($data['id']);
    if (isset($_SERVER['HTTP_REFERER'])) 
      redirect($_SERVER['HTTP_REFERER']);
    else
      redirect("/");
  }
}

?>
